using UnityEngine;
using UnityEngine.UI;
using System.Collections;

namespace UITools
{
	[AddComponentMenu("UITools/LoadScene")]
	public class LoadScene : MonoBehaviour
	{
		public bool AsyncLoad = false;
		public string SceneName = "";

		AsyncOperation mAsyncLoading = null;

		// Use this for initialization
		void Start ()
		{
			Button btn = gameObject.GetComponent<Button> ();
			if (btn != null) {
				btn.onClick.AddListener (Load);
			}
		}

		// Update is called once per frame
		void Update ()
		{

		}

		public void Load()
		{
			if (UILoading.Instance != null)
				UILoading.Instance.LoadScene(SceneName, AsyncLoad);
			else
			{
				if (AsyncLoad)
					StartCoroutine(Load(SceneName));
				else
					Application.LoadLevel(SceneName);
			}
		}

		public IEnumerator Load(string name)
		{	
			mAsyncLoading = Application.LoadLevelAsync(name);
			yield return mAsyncLoading;
		}
	}
}

